
package com.dps.omc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.model.*;
import com.dps.omc.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class AgentNodeAction extends AbstractAdminController<IAgentNodeService>{

    @Autowired
    private IAgentNodeService agentNodeService;
    @Autowired
    private IGroupTypeService groupTypeService;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IDbServerService dbServerService;
    @Autowired
    private IBizTypeService bizTypeService;
    @Autowired
    private IBizUnitService bizUnitService;
    
    @RequestMapping(value="/agentnode/list")
    public String list(AgentNode agentNode,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(agentNode); 	
    	BizData4Page<AgentNode> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("agentNode", agentNode);
    	return "/module/agentnode/list";
    }    
    
    @RequestMapping(value="/agentnode/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){


        List<GroupType> groupTypes = groupTypeService.findAll();
        List<Agent> agents =  agentService.findAll();
        List<DbServer> dbServers = dbServerService.findAll();
        List<BizType> bizTypes = bizTypeService.findAll();
        List<BizUnit> bizUnits =  bizUnitService.findAll();
        model.addAttribute("groupTypes",groupTypes);
        model.addAttribute("agents", agents);
        model.addAttribute("dbServers", dbServers);
        model.addAttribute("bizTypes", bizTypes);
        model.addAttribute("bizUnits", bizUnits);
        model.addAttribute("agentNode", new AgentNode());
        return "module/agentnode/edit";
    }    
    
    @RequestMapping(value="/agentnode/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	AgentNode agentNode = agentNodeService.fetch(id);
        List<GroupType> groupTypes = groupTypeService.findAll();
        List<Agent> agents =  agentService.findAll();
        List<DbServer> dbServers = dbServerService.findAll();
        List<BizType> bizTypes = bizTypeService.findAll();
        List<BizUnit> bizUnits =  bizUnitService.findAll();
        model.addAttribute("groupTypes",groupTypes);
        model.addAttribute("agents", agents);
        model.addAttribute("dbServers", dbServers);
        model.addAttribute("bizTypes", bizTypes);
        model.addAttribute("bizUnits", bizUnits);
    	model.addAttribute("agentNode", agentNode);
        return "module/agentnode/edit";
    }    
    
    @RequestMapping(value="/agentnode/save")
    @ResponseBody
    public String save(HttpServletRequest request,AgentNode agentNode){
		boolean isAddNew = StringUtils.isBlank(agentNode.getId())?true:false;
		if(isAddNew)
		{
			agentNode.setId(IdGenerator.createNewId());
            this.getNode(agentNode);
			agentNodeService.add(agentNode);
		}
		else
		{
			agentNodeService.update(agentNode);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }

    private AgentNode getNode(AgentNode agentNode)
    {
        if(agentNode.getGroupType().equals("DBGROUP"))
        {
            DbServer dbServer = dbServerService.fetch(agentNode.getNodeId());
            agentNode.setIp(dbServer.getDbHost());
            agentNode.setAlias(dbServer.getAlias());
        }
        return agentNode;
    }
    
    @RequestMapping(value="/agentnode/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	agentNodeService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }

    protected Map getQueryMap(AgentNode agentNode)
    {
        Map<String, Object> conditions = new HashMap();

        conditions.put("nodeId", agentNode.getNodeId());
        conditions.put("agentId", agentNode.getAgentId());
        conditions.put("ip", agentNode.getIp());
        conditions.put("alias", agentNode.getAlias());
        conditions.put("bizUnit", agentNode.getBizUnit());
        conditions.put("bizType", agentNode.getBizType());
        conditions.put("groupType", agentNode.getGroupType());
        conditions.put("sysRank", agentNode.getSysRank());
        conditions.put("opSystem", agentNode.getOpSystem());
        conditions.put("createrId", agentNode.getCreaterId());
        conditions.put("createrName", agentNode.getCreaterName());
        conditions.put("createDate", agentNode.getCreateDate());
        return conditions;
    }

    @Override
    protected IAgentNodeService getMainService() {
        return agentNodeService;
    }

    @Override
    protected String getMainObjName() {
        return "agentnode";
    }

    @Override
    protected String getViewTitle() {
        return "agentnode";
    }
}
